<?php
$loader->requireOnce('includes/LabAssignment/LabAssignmentProcessor.class.php');

/**
 * Processor for lab admin
 * @package modules.dept_laboratory.includes
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabAdminProcessor {
	private $controller;
	private $logger ;
	function __construct($controller) {
		$this->controller = $controller;
		$this->logger = LoggerManager::getLogger('modules.dept_laboratory.includes.LabAdmin.LabAdminProcessor');
	}
	/**
	 * Render assignment page.
	 * @return html
	 */
	public function listOfLabGroup() {
		$labAssignmentModel = new LabAssignmentModel();
        return $labAssignmentModel->labGroupModelToArray();
	}
	/**
	 * update or addnew a group.
	 * @param $id
	 * @param $text
	 * @return unknown_type
	 */
	
	public function saveLabGroup($id, $text) {
	    $result = 0;
	    $labGroupObj = Celini::newOrdo('LabGroup');
	    if (trim($id) == '') {
	        //addnew	        
	        $result = $labGroupObj->addLabGroup($text);
	    } else {
	        //update	        
	        $result = $labGroupObj->updateLabGroup($id, $text);	        
	    }
	    return $result;
	}
	
    public function updateLabService($serviceId, $serviceName, $price, $disabled) {
        $serviceObj = Celini::newOrdo('Service');
        $serviceObj->setup($serviceId);
            
        if ($serviceId == 0 || $serviceId == '') {
             $data = array(
                'price'     =>  $price,
                'disabled'  =>  $disabled
            );
            $langs = L10nHelper::getSupportedLocales();
            foreach ($langs as $lang=>$country) {
            	$lang = L10nHelper::changeField($lang,"name");
            	$data[$lang] = $serviceName;
            }
            
        } else {
        	$lang = L10nHelper::changeField(L10nHelper::getLocale(),"name");
             $data = array(
            'price'     =>  $price,
            'disabled'  =>  $disabled
            );
            $data[$lang] = $serviceName;
        }
        
        $serviceObj->populateArray($data);
        $serviceObj->persist();
    }
    
    public function addLabService($labGroupId, $serviceName, $price, $disabled) {
        $data = array(
            'price'     =>  $price,
            'disabled'  =>  $disabled,
            'item_type' =>  EnumerationConstant::PARACLINIC_LAB
        );
        $lang = L10nHelper::changeField(L10nHelper::getLocale(),"name");
        $data[$lang] = $serviceName;

        
        $serviceObj = Celini::newOrdo('Service');
        $serviceObj->populateArray($data);
        $serviceObj->persist();
        $serviceId = $serviceObj->get_id();
        
        $labServiceMapObj = Celini::newOrdo('LabService');
        $labServiceMapObj->populateArray(array(
            'lab_group_id'  => $labGroupId,
            'service_id'    => $serviceId        
        ));
        
        $labServiceMapObj->persist();
        
        return $serviceId;        
    }
	
	public function listLabServiceOfSelectedGroup($id) {
	    $servicesObj = Celini::newOrdo("LabService");
        $result = $servicesObj->loadLabServices4Admin($id);        
        return $result;
	}
    public function listLabResultOfSelectedService($labServiceId) {
        $servicesObj = Celini::newOrdo("LabItemFactor");
        $result = $servicesObj->listLabResultItemsOfService($labServiceId); 
               
        foreach ($result as &$item) {
        	$item['enum_unit_text'] = Utils::getEnumTextByKey(EnumerationConstant::UNIT,
        	$item['enum_unit']);
        	$item['enum_data_type_text'] = Utils::getEnumTextByKey(EnumerationConstant::LAB_DATATYPE,
            $item['enum_data_type']);
        }        
        return $result;
    }
    
    public function updateLabResult($labResultId, $resultName,
     $labResultNormalMale, $labResultNormalFemale, $unit, $dataType) {
        $servicesObj = Celini::newOrdo("LabItemFactor");
        $servicesObj->setup($labResultId);
        
        if ($servicesObj->get_id() == 0 || $servicesObj->get_id() == '') {
            $data = array(
	            'enum_unit'         => $unit,
	            'enum_data_type'    => $dataType,
	            'normal_result_male' => $labResultNormalMale,
	            'normal_result_female' => $labResultNormalFemale
            );
            $langs = L10nHelper::getSupportedLocales();
        	foreach ($langs as $lang=>$country) {
            	$lang = L10nHelper::changeField($lang,"name");
            	$data[$lang] = $resultName;
            }
        } else {      
            $data = array(
            'enum_unit' => $unit,
            'enum_data_type' => $dataType,
            'normal_result_male' => $labResultNormalMale,
            'normal_result_female' => $labResultNormalFemale
            );
            $lang = L10nHelper::changeField(L10nHelper::getLocale(),"name");
            $data[$lang] 	=	$resultName;
        }
        $servicesObj->populateArray($data);
        $servicesObj->persist(); 
    }
    
    public function addLabResult($labServiceId, $resultName,
     $labResultNormalMale, $labResultNormalFemale, $unit, $dataType) {
     	
        $servicesObj = Celini::newOrdo("LabItemFactor");        
        $data = array(
            'service_id' => $labServiceId,
            'enum_unit' => $unit,
            'enum_data_type' => $dataType,
            'normal_result_male' => $labResultNormalMale,
            'normal_result_female' => $labResultNormalFemale
        );
        
      	$langs = L10nHelper::getSupportedLocales();
        foreach ($langs as $lang=>$country) {
            $lang = L10nHelper::changeField($lang,"name");
            $data[$lang] = $resultName;
      	}
        $servicesObj->populateArray($data);
        $servicesObj->persist();
        return $servicesObj->get_id();
    }
}
?>